76// pages/information/information.js
const app = getApp()
var uploadImage = require('../../utils/uploadFile.js');
var util = require('../../utils/util.js');
import { HTTP } from '../../utils/https.js'
let http = new HTTP()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    sexarray: [
        { id: 1, name: '男' },
        { id: 2, name: '女' }      
      ],
    signing_type:0, //是否面议
    sex:'性别',
    sex_id:null,
    date:'', //出身年月
    headurl:'../../image/upimg.png', //头像图片
    jobarray:'',
    job: '职业', //职业值
    job_id: '', //职业id
    birthday:'出生年月', //出身年月
    heightval:'', //获取的身高
    weightval:Number,//获取的体重
    nameval:String, //获取的姓名
    schoolval:String, //获取的学校名称
    informationval:String, //获取的个人简介
    buttontext:'下一步',
    disabled:false, //是否可编辑
  //城市搜索开始
    procive: [],
    city: [],
    region: [],
    provinces: [],
    cityid: '',
    citys: [],
    provinceid: '',
    countys: [],
    countysid: '',
    key: '',
    checkdirects:'',
  //城市搜索结束
    checkname:'checkname',
    hiddentishi: true,
    checkrole:Number, //判断是什么角色
    jobwrite:'', //企业职业
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   //若是修改资料
    this.setData({
      checkname: options.name,
      checkrole: options.check
    })
    if (options.name == 'personinform'){
      var that = this;
      http.request({
        url: 'mini/v1/personal/info',
        success: function (res) {
          that.setData({
            hiddentishi:true,
            headurl: res.data.headimgurl,
            sex: res.data.sex == '2' ? '女' :'男',
            job: res.data.career,
            birthday: res.data.birthday,
            heightval: res.data.height,
            weightval: res.data.weight,
            nameval:res.data.name,
            schoolval:res.data.school,
            city:res.data.region,
            informationval:res.data.introduction,
            signing_type: res.data.signing_type,
            signing_priceval: res.data.signing_price,
            buttontext:'修改',
            disabled:true,
            sex_id: res.data.sex,
            job_id: res.data.career_id,
            provinceid: res.data.region_province_id,
            cityid: res.data.region_city_id,
            countysid: res.data.region_area_id,
            jobwrite:res.data.career_company
          })
        }
      });
      return false
    }else{
      this.setData({
        hiddentishi:false
      })
    }
    //获取资料
        var that = this;
        var data = {
          group_by: ['careerList','regionTree']
        }
        http.request({
          url: 'mini/v1/personal/make',
          data: data,
          methods: 'post',
          success: function (res) {
            //省份城市
            var region = res.data.regionTree;

            that.setData({
              region: region,
              jobarray: res.data.careerList
            });
            const provinces = [[], [], []];
            const citys = [[], []];
            const countys = [];
            //console.log("初始化省份");
            for (let i = 0; i < that.data.region.length; i++) {
              var obj = new Object;
              obj.itemid = that.data.region[i].region_id;
              obj.name = that.data.region[i].name
              provinces[0].push(obj);
            }
           
            that.setData({
              countysid: that.data.region[0].children[0].children[0].region_id,
              cityid: that.data.region[0].children[0].region_id,
              provinceid: that.data.region[0].region_id,
              province: that.data.region[0].name,
              city: that.data.region[0].children[0].name,
              countys: that.data.region[0].children[0].children[0].name
            })
            
            //console.log("初始化市区");
            for (let i = 0; i < that.data.region[0].children.length; i++) {
              var obj = new Object;
              obj.itemid = that.data.region[0].children[i].region_id;
              obj.name = that.data.region[0].children[i].name;
              provinces[1].push(obj);
            }

            //console.log("初始化区域");
            for (let i = 0; i < that.data.region[0].children[0].children.length; i++) {
              var obj = new Object;
              obj.itemid = that.data.region[0].children[0].children[0].region_id;
              obj.name = that.data.region[0].children[0].children[i].name;
              provinces[2].push(obj);
            }

            that.setData({
              'provinces': provinces,
              'citys': citys,
              'province': that.data.region[0].name,
              'city': that.data.region[0].children[0].name,
              'countys': that.data.region[0].children[0].children[0].name
            });
          }
        })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  /**
   * 上传头像
   */
  //选择照片
  choose: function () {
    var name = 'headurl';
    var that = this;
    util.upimg(name, that);
  },
  //选择城市
  bindMultiPickerColumnChange: function (e) {
    var that = this;
    if (e.detail.column == 0) {
      that.setData({
        "provinces[1]": that.data.region[e.detail.value].children,
        "provinceid": that.data.region[e.detail.value].region_id,
        'key': e.detail.value,
        'province': that.data.region[e.detail.value].name,
        "provinces[2]": that.data.region[e.detail.value]['children'][0].children
      });
    } else if (e.detail.column == 1) {
      that.setData({
        "provinces[2]": that.data.region[that.data.key]['children'][e.detail.value].children,
        "cityid": that.data.region[that.data.key]['children'][e.detail.value].region_id,
        "city": that.data.region[that.data.key]['children'][e.detail.value].name
      });
    } else {
      that.setData({
        "countys": that.data.provinces[2][e.detail.value].name,
        "countysid": that.data.provinces[2][e.detail.value].region_id
      });
    }
  },
  bindMultiPickerChange: function (e) { //城市选择
    var that = this;
    var key = e.detail.value[0];
    var key0 = e.detail.value[1];
    var key1 = e.detail.value[2];

    if (key0 == null) {
      key0 = 0;
    }

    if (key1 == null) {
      key1 = 0;
    }

    that.setData({
      "countys": that.data.region["" + key + ""]['children']["" + key0 + ""]['children']["" + key1 + ""]['name'],
      "countysid": that.data.region["" + key + ""]['children']["" + key0 + ""]['children']["" + key1 + ""]['region_id'],
      "cityid": that.data.region["" + key + ""]['children']["" + key0 + ""]['region_id'],
      "city": that.data.region["" + key + ""]['children']["" + key0 + ""]['name'],
      "provinceid": that.data.region["" + key + ""]['region_id'],
      "province": that.data.region["" + key + ""]['name']
    });
  },
  formSubmit:function(e){
    var detail_infor = e.detail.value;
    //上传头像
      if (this.data.headurl == "../../image/upimg.png"){
        wx.showToast({
          title: '请上传您的头像，头像必传！',
          icon: 'none',
          duration: 2000
        });
        return;
      }
    //填写姓名
      if (!detail_infor.name) {
        wx.showToast({
          title: '请填写姓名！',
          icon: 'none',
          duration: 2000
        });
        return;
      }
    
    //选择性别
      if (this.data.sex_id == null) {
        wx.showToast({
          title: '请选择性别！',
          icon: 'none',
          duration: 2000
        });
        return;
      }
    //选择职业
        if (!this.data.job_id  && this.data.checkrole == 1) {
          wx.showToast({
            title: '请选择职业！',
            icon: 'none',
            duration: 2000
          });
          return;
        }

      //选择职业
      if (detail_infor.jobwrite == ''  && this.data.checkrole == 2) {
        wx.showToast({
          title: '请填写职业！',
          icon: 'none',
          duration: 2000
        });
        return;
      }
    
      
    //选择出生年月
     if (this.data.birthday =="出生年月" && this.data.checkrole == 1) {
        wx.showToast({
          title: '请选择出生年月！',
          icon: 'none',
          duration: 2000
        });
        return;
      }
    //身高
      if (!detail_infor.height && this.data.checkrole == 1) {
        wx.showToast({
          title: '请填写身高！',
          icon: 'none',
          duration: 2000
        });
        return;
      }
    //体重
      if (!detail_infor.weight && this.data.checkrole == 1) {
        wx.showToast({
          title: '请填写体重！',
          icon: 'none',
          duration: 2000
        });
        return;
      }
    //毕业院校
     if (!detail_infor.school && this.data.checkrole == 1) {
        wx.showToast({
          title: '请填写毕业院校！',
          icon: 'none',
          duration: 2000
        });
        return;
      }
    //个人简介
     if (!detail_infor.information) {
        wx.showToast({
          title: '请填写个人简介！',
          icon: 'none',
          duration: 2000
        });
        return;
      }
    //薪资范围
      if (this.data.signing_type ==0 && this.data.checkrole == 1){
        if (!detail_infor.signing_price){
          wx.showToast({
            title: '请填写薪资！',
            icon: 'none',
            duration: 2000
          });
          return;
        }
      }

     var signing_price = 0;
     if (this.data.signing_type == 0) {
       signing_price = detail_infor.signing_price
     }else{
       signing_price = 0
     }
     
    //提交
        var that = this;
        let data = { 
          headimgurl: this.data.headurl,
          name: detail_infor.name,
          sex: this.data.sex_id,
          career_id:this.data.checkrole == 1 ? this.data.job_id : '' ,
          birthday: this.data.checkrole == 1 ? this.data.birthday : '2020-5-25',
          height: detail_infor.height,
          weight: detail_infor.weight,
          school: detail_infor.school,
          region_province_id:this.data.provinceid,
          region_city_id: this.data.cityid,
          region_area_id: this.data.countysid,
          signing_type:this.data.checkrole == 1 ? this.data.signing_type : '0',
          signing_price: signing_price,
          introduction: detail_infor.information,
          career_company:this.data.checkrole == 2 ? detail_infor.jobwrite : '',
        };

        http.request({
          url: that.data.checkname !== 'personinform' ? 'mini/v1/personal/make' :'mini/v1/personal/info_patch',
          data: data,
          methods:'post',
          success: function (res) {
            //若是完善信息
              if (that.data.checkname !== 'personinform'){
                wx.removeStorageSync('checklogin');
                wx.setStorageSync('checklogin', 'checklogin');
                var type = that.data.checkrole == 1 ? 'personal' : 'company';
                if(type == 'company'){
                  wx.switchTab({
                    url: '../../pages/person/person'
                  });
                }else{
                  wx.navigateTo({
                    url: '../certificates/certificates?type='+type
                  })
                }
                
              }
            //修改资料
              else{
                wx.switchTab({
                  url: '../../pages/person/person'
                });
              }
          }
        })
  },
  //选择职业
  bindPickerChangejob(e){
    this.setData({
      job: this.data.jobarray[e.detail.value].name,
      job_id: this.data.jobarray[e.detail.value].id
    })
  },
  //选择出生年月
  bindPickerChangeyear(e){
    this.setData({
      birthday: e.detail.value
    })
  },
  //选择性别
  bindPickerChangesex(e){
    this.setData({
      sex: this.data.sexarray[e.detail.value].name,
      sex_id: this.data.sexarray[e.detail.value].id
    })
  },
  onShareAppMessage: function () {
    return util.share()
  },
  //是否面议薪资
  checkboxChange(e){
    if (e.detail.value[0]){
      this.setData({
        signing_type: 1
      })
    }else{
      this.setData({
        signing_type:0
      })
    }
  }
})